﻿using duqin.Common;
using duqin.Entities;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace duqin.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RecommendsController : ControllerBase
    {
        private readonly ILogger<RecommendsController> _logger;
        private readonly IFreeSql _freeSql;

        public RecommendsController(ILogger<RecommendsController> logger, IFreeSql freeSql)
        {
            _logger = logger;
            _freeSql = freeSql;
        }

        // GET: api/<RecommendsController>
        [HttpGet]
        public async Task<IActionResult> Get(int page = 1, int limit = 10)
        {
            var types = new[] { nameof(User), nameof(Song), nameof(Article), nameof(Video) };


            var users = await _freeSql.Select<User>().Where(x => x.Type == "DQ_SINGER").Page(page, limit).ToListAsync();
            var songs = await _freeSql.Select<Song>().LeftJoin(a => a.User != null && a.User.Id == a.UserId).Page(page, limit).ToListAsync();
            var articles = await _freeSql.Select<Article>().LeftJoin(a => a.User != null && a.User.Id == a.UserId).Page(page, limit).ToListAsync();
            var videos = await _freeSql.Select<Video>().LeftJoin(a => a.User != null && a.User.Id == a.UserId).Page(page, limit).ToListAsync();

            var randModels = new Dictionary<int, string>();
            for (int i = 0; i < limit; i++)
            {
                var index = new Random().Next(0, types.Length - 1);
                randModels.Add(i, types[index]);
            }

            var data = new List<(string, object)>();
            foreach (var (key, value) in randModels)
            {
                switch (value)
                {
                    case nameof(User):
                        if (users.Count > key)
                            data.Add(($"{nameof(User).ToLower()}Entity", users[key]));
                        break;
                    case nameof(Song):
                        if (songs.Count > key)
                            data.Add(($"{nameof(Song).ToLower()}Entity", songs[key]));
                        break;
                    case nameof(Article):
                        if (articles.Count > key)
                            data.Add(($"{nameof(Article).ToLower()}Entity", articles[key]));
                        break;
                    case nameof(Video):
                        if (videos.Count > key)
                            data.Add(($"{nameof(Video).ToLower()}Entity", videos[key]));
                        break;
                }
            }

            return ServerResponse.Yes(data);
        }


        [ApiExplorerSettings(IgnoreApi = true)]
        // GET api/<RecommendsController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        // POST api/<RecommendsController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        // PUT api/<RecommendsController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        // DELETE api/<RecommendsController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
